package com.ruoyi.ax.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

import com.ruoyi.ax.domain.AxMemberLevel;
import com.ruoyi.ax.domain.AxOrder;
import com.ruoyi.ax.service.IAxMemberLevelService;
import com.ruoyi.ax.service.IAxOrderService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.MoneyTypeEnum;
import com.ruoyi.common.enums.OrderStatus;
import com.ruoyi.common.enums.changeSouceType;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ax.mapper.AxMemberOrderMapper;
import com.ruoyi.ax.domain.AxMemberOrder;
import com.ruoyi.ax.service.IAxMemberOrderService;

/**
 * 会员卡订单Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-06-10
 */
@Service
public class AxMemberOrderServiceImpl implements IAxMemberOrderService 
{
    @Autowired
    private AxMemberOrderMapper axMemberOrderMapper;


    @Autowired
    private IAxMemberLevelService axMemberLevelService;


    @Autowired
    private ISysUserService userService;

    @Autowired
    private   IAxOrderService iAxOrderService;


    /**
     * 查询会员卡订单
     * 
     * @param id 会员卡订单主键
     * @return 会员卡订单
     */
    @Override
    public AxMemberOrder selectAxMemberOrderById(Long id)
    {
        return axMemberOrderMapper.selectAxMemberOrderById(id);
    }

    /**
     * 查询会员卡订单列表
     * 
     * @param axMemberOrder 会员卡订单
     * @return 会员卡订单
     */
    @Override
    public List<AxMemberOrder> selectAxMemberOrderList(AxMemberOrder axMemberOrder)
    {
        return axMemberOrderMapper.selectAxMemberOrderList(axMemberOrder);
    }

    /**
     * 新增会员卡订单
     * 
     * @param axMemberOrder 会员卡订单
     * @return 结果
     */
    @Override
    public int insertAxMemberOrder(AxMemberOrder axMemberOrder)
    {
        axMemberOrder.setCreateTime(DateUtils.getNowDate());
        return axMemberOrderMapper.insertAxMemberOrder(axMemberOrder);
    }

    @Override
    public int buyAxMemberOrder(AxMemberOrder axMemberOrder) {

        Long userId=  SecurityUtils.getUserId();
        SysUser sysUser= userService.selectUserById(userId);
        AxMemberLevel axMemberLevel= axMemberLevelService.selectAxMemberLevelById(axMemberOrder.getGoodId());
        axMemberOrder.setPrice(new BigDecimal(axMemberLevel.getCardPrice()));
        axMemberOrder.setOrderNum("1");
        axMemberOrder.setOrderNum(DateUtils.parseDateToStr(DateUtils.YYYYMMDDHHMMSS,new Date())+iAxOrderService.getMillseconds());
        axMemberOrder.setSysUserId(userId);
        axMemberOrder.setPayMonery(axMemberLevel.getCardPrice());
        AxOrder axOrder=new AxOrder();
        if(sysUser.getMemberId().equals(axMemberOrder.getGoodId())){
            throw  new GlobalException("您已是"+axMemberLevel.getMemberName()+";无需购买");
        }
         this.insertAxMemberOrder(axMemberOrder);
        if(MoneyTypeEnum.余额.getCode()==axMemberOrder.getMoneyType()){

            if(new BigDecimal(sysUser.getRechargeAmount()).compareTo(new BigDecimal(axMemberLevel.getCardPrice()))<0){
                throw  new GlobalException("抱歉，余额不足");
            }
            sysUser.setMemberId(axMemberLevel.getId());
            sysUser.setMemberName(axMemberLevel.getMemberName());
            BigDecimal rechargeAmount= new BigDecimal(sysUser.getRechargeAmount());
            sysUser.setRechargeAmount((rechargeAmount.subtract(new BigDecimal(axMemberLevel.getCardPrice()))).setScale(2, RoundingMode.HALF_UP).toString());
            userService.updateUser(sysUser);
            // 增加变动明细；
            axMemberOrder.setOrderState(Long.valueOf(OrderStatus.待发货.getCode()));
            BeanUtils.copyProperties(axMemberOrder,axOrder);
           return  iAxOrderService.adAccountChangeRecord(axOrder,"-"+axMemberLevel.getCardPrice(),userId
                    ,"","", changeSouceType.购买会员卡.getCode(),changeSouceType.购买会员卡.name());
        }else{

             return 0;

        }


    }

    /**
     * 修改会员卡订单
     * 
     * @param axMemberOrder 会员卡订单
     * @return 结果
     */
    @Override
    public int updateAxMemberOrder(AxMemberOrder axMemberOrder)
    {
        axMemberOrder.setUpdateTime(DateUtils.getNowDate());
        return axMemberOrderMapper.updateAxMemberOrder(axMemberOrder);
    }

    /**
     * 批量删除会员卡订单
     * 
     * @param ids 需要删除的会员卡订单主键
     * @return 结果
     */
    @Override
    public int deleteAxMemberOrderByIds(Long[] ids)
    {
        return axMemberOrderMapper.deleteAxMemberOrderByIds(ids);
    }

    /**
     * 删除会员卡订单信息
     * 
     * @param id 会员卡订单主键
     * @return 结果
     */
    @Override
    public int deleteAxMemberOrderById(Long id)
    {
        return axMemberOrderMapper.deleteAxMemberOrderById(id);
    }
}
